#/usr/bin/env python
# -*- coding: UTF-8 -*-

import wx

ID_ABOUT  = 101
ID_EXIT   = 102
ID_CUT    = 103
ID_COPY   = 104
ID_STATUS = 105

###
class MyPopupMenu(wx.Menu):
    def __init__(self, parent):
        wx.Menu.__init__(self)

        self.parent = parent

        minimize = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.AppendItem(minimize)
        self.Bind(wx.EVT_MENU, self.OnMinimize, id=minimize.GetId())

        close = wx.MenuItem(self, wx.NewId(), 'Close')
        self.AppendItem(close)
        self.Bind(wx.EVT_MENU, self.OnClose, id=close.GetId())


    def OnMinimize(self, event):
        self.parent.Iconize()

    def OnClose(self, event):
        self.parent.Close()

###
class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title,
          size = (400, 400),
          style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
        self.SetIcon(wx.Icon('icons/app.ico', wx.BITMAP_TYPE_ICO))
        self.control = wx.TextCtrl(self, 1, style = wx.TE_MULTILINE)
        #
        self.SetBackgroundColour('green')
        #----
        self.statusbar = self.CreateStatusBar()
        #----
        filemenu = wx.Menu()
        about = wx.MenuItem(filemenu, ID_ABOUT, "&About\tCtrl+A", "Infomation about this program")
        about.SetBitmap(wx.Bitmap('pngs/about.png'))
        filemenu.AppendItem(about)
        #filemenu.Append(ID_ABOUT, "&About", "Infomation about this program")
        filemenu.AppendSeparator()
        
        imp = wx.Menu()
        imp.Append(-1, 'Import newsfeed list...')
        imp.Append(-1, 'Import bookmarks...')
        imp.Append(-1, 'Import mail...')

        filemenu.AppendMenu(-1, 'I&mport', imp)
        
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT, "&Exit", "Terminate the program")
        #----
        editmenu = wx.Menu()
        editmenu.Append(ID_CUT, "&Cut", "Cut the selected text")
        editmenu.Append(ID_COPY, "&Copy", "Copy the selected text")
        #----
        viewmenu = wx.Menu()
        self.stb = viewmenu.Append(ID_STATUS, "Show Status Bar", "Show Status Bar", kind=wx.ITEM_CHECK) 
        viewmenu.Check(ID_STATUS, True)
        #----
        menubar = wx.MenuBar(wx.MB_DOCKABLE)
        menubar.Append(filemenu, "&File")
        menubar.Append(editmenu, "&Edit")
        menubar.Append(viewmenu, "&View")
        self.SetMenuBar(menubar)
        ###
        toolbar = self.CreateToolBar()
        toolbar.AddLabelTool(wx.ID_EXIT, 'exit', wx.Bitmap('pngs/network.png'))
        toolbar.SetToolBitmapSize(size = wx.Size(32, 32))
        toolbar.Realize()
        #----
        wx.EVT_MENU(self, ID_ABOUT, self.onAbout)
        wx.EVT_MENU(self, ID_EXIT, self.onExit)
        self.Bind(wx.EVT_MENU, self.onCut, id=ID_CUT)
        self.Bind(wx.EVT_MENU, self.toggleStatusBar, id=ID_STATUS)
        self.Bind(wx.EVT_RIGHT_DOWN, self.onRightDown)
        self.Bind(wx.EVT_TOOL, self.onExit, id=wx.ID_EXIT)
        #----
        self.Centre()
        self.Show(True)
        
    #----
    def onAbout(self, event):
        d = wx.MessageDialog(self, "A Sample Editor \n in wxpython ",
                              "About Sample Editor",
                              wx.OK)
        d.ShowModal()
        d.Destroy()
        
    #----
    def onExit(self, event):
        self.Close(True)
        
    #----
    def onCut(self, event):
        wx.MessageBox('Not implemented yet', 'Info')
        
    #----
    def toggleStatusBar(self, event):
        if self.stb.IsChecked():
            self.statusbar.Show()
        else:
            self.statusbar.Hide()
        self.SendSizeEvent()
    
    def onRightDown(self, event):
        self.PopupMenu(MyPopupMenu(self), event.GetPosition())
        
class MyApp(wx.App):
    """Application class."""
    def OnInit(self):
        self.frame = MainWindow(None, -1, "HyWxEditor")
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

def main():
    app = MyApp()
    app.MainLoop()

if __name__ == '__main__':
    main()